package com.demo.mvp.interactor;

import android.os.Handler;
import android.text.TextUtils;

import com.demo.mvp.bean.UserBean;
import com.demo.mvp.model.IUserModel;
import com.demo.mvp.model.UserModel;
import com.demo.mvp.view.OnLoginFinishedListener;

/**
 * 登陆、注册 交互器
 * @author Administrator
 *
 */
public class LoginInteractor implements ILoginInteractor
{
	private IUserModel userModel;
	
	public LoginInteractor(){
		userModel = new UserModel();
	}

	@Override
	public void login(final String username, final String password,final OnLoginFinishedListener listener)
	{
		// TODO Auto-generated method stub
		// Mock login. I'm creating a handler to delay the answer a couple of seconds
        new Handler().postDelayed(new Runnable() {
            @Override public void run() {
                boolean error = false;
                if (TextUtils.isEmpty(username)){
                    listener.onUsernameError();
                    error = true;
                }
                if (TextUtils.isEmpty(password)){
                    listener.onPasswordError();
                    error = true;
                }
                error = userModel.login(username, password);
                if (!error){
                    listener.onSuccess();
                }
            }
        }, 2000);
	}

	@Override
	public void regist(final UserBean userBean,final OnLoginFinishedListener listener)
	{
		new Handler().postDelayed(new Runnable() {
            @Override public void run() {
                boolean error = false;
                if(userBean==null){
                	listener.onUsernameError();
                    listener.onPasswordError();
                    error = true;
                }else{
	                if (TextUtils.isEmpty(userBean.getName())){
	                    listener.onUsernameError();
	                    error = true;
	                }
	                if (TextUtils.isEmpty(userBean.getPassword())){
	                    listener.onPasswordError();
	                    error = true;
	                }
                }
                if (!error){
                	userModel.add(userBean);
                    listener.onSuccess();
                }
            }
        }, 2000);
	}

}
